package org.hdl.anima.session;

import java.util.Map;

import org.hdl.anima.common.module.BasicModule;
import org.jboss.netty.util.internal.ConcurrentHashMap;


/**
 * 
 * @author qiuhd
 * @since  2014年8月15日
 */
public class BackendSessionMgr extends BasicModule{
	
	private Map<Integer, BackendSession> backendSessions;
	
	private SessionCloseListener closeListener = new SessionCloseListener() {
		@Override
		public void onSessionClosed(Object session) {
			if (session != null) {
				BackendSession backendSession = (BackendSession) session;
				backendSessions.remove(backendSession.getIdentity());
			}
		}
	};
	
	public BackendSessionMgr(String moduleName) {
		super(moduleName);
		backendSessions = new ConcurrentHashMap<Integer, BackendSession>();
	}
	
	public BackendSession getSession(int identity) {
		return backendSessions.get(identity) ;
	}
	
	public BackendSession createSession(String rsid,int identity,String remoteAddress,int remotePort,String localAddress,int localPort) {
		BackendSession session = new BackendSession(application,rsid, identity,remoteAddress,remotePort,localAddress,localPort);
		session.setlistener(closeListener);
		backendSessions.put(identity, session);
		return session;
	}
}

